<?php

namespace App\Services\Postman;

use App\Exceptions\Response\ServiceException;
use App\Models\Game\Config\System;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\Http;

class GameService
{

    /**
     * 获取实时数据【Player】
     *
     * @var string
     */
    const URI_REALTIME_PLAYER_INFO = '/gm/client/getRealtimePlayerInfo';

    /**
     * 修改玩家角色
     *
     * @var string
     */
    const URI_SET_PLAYER_ROLE = '/gm/client/setPlayerRole';

    /**
     * 修改玩家权限
     *
     * @var string
     */
    const URI_SET_PLAYER_ACCESS = '/gm/client/setPlayerAccess';

    /**
     * 修改玩家点控模型【slo】
     *
     * @var string
     */
    const URI_SET_PLAYER_MODEL = '/gm/client/setPlayerModel';

    /**
     * 修改玩家点控修正【fish】
     *
     * @var string
     */
    const URI_SET_PLAYER_FISH_RATIO = '/gm/client/setPlayerFishRatio';

    /**
     * 发送玩家邮件
     *
     * @var string
     */
    const URI_SEND_PLAYER_MAIL = '/gm/client/sendPlayerMail';

    /**
     * 发送全服邮件
     *
     * @var string
     */
    const URI_SEND_SERVER_MAIL = '/gm/client/sendServerMail';

    /**
     * 验证钱包接口
     *
     * @var string
     */
    const URI_CHECK_WALLET = '/gm/client/checkSeamlessWalletApi';

    /**
     * 订单状态
     *
     * @var string
     */
    const URI_TRANSFER_STATUS = '/gm/client/coinTransactions';

    /**
     * 订单取消【钱包(实时)】
     *
     * @var string
     */
    const URI_WALLET_TIMELY_CANCEL = '/gm/client/cancelISWalletOrder';

    /**
     * 刷新配置
     *
     * @var string
     */
    const URI_REFRESH_CONFIG = '/gm/client/refreshConfig';

    /**
     * 制作链接地址
     *
     * @param string $uri
     * @return string
     */
    protected static function makeUri(string $uri): string
    {
        return rtrim(config('app.domain.game'), '/') . $uri;
    }

    /**
     * 制作 CURL 对象
     *
     * @return PendingRequest
     * @throws ServiceException
     */
    protected static function makeHttp(): PendingRequest
    {
        $config = System::findByType(System::TYPE_GM_TOKEN);
        // 游戏服务 token 不存在
        if (empty($config)) {
            throw new ServiceException(ServiceException::SERVICE_GAME_SERVICE_NOT_EXIST_TOKEN);
        }

        // 超时时间 20 秒
        return Http::timeout(20)->withHeaders([
            'token' => $config->info['token']
        ]);
    }

    /**
     * POST 公共请求
     *
     * @param string $uri
     * @param array $data
     * @return mixed|null
     * @throws ServiceException
     */
    public static function post(string $uri, array $data = [])
    {
        try {
            $response = self::makeHttp()->post(self::makeUri($uri), $data);
        } catch (\Illuminate\Http\Client\ConnectionException $e) {
            throw new ServiceException(ServiceException::SERVICE_GAME_SERVICE_REQUEST_TIMEOUT);
        }
        // HTTP错误
        if (!$response->successful()) {
            throw new ServiceException(ServiceException::SERVICE_GAME_SERVICE);
        }
        // 返回数据
        $result = $response->json();

        // 游戏服务错误
        if ($result['error']['code'] < 0) {
            throw new ServiceException($result['error']['code'], $result['error']['msg']);
        }

        return $result['data'] ?? null;
    }

    /**
     * 通知游戏端配置修改
     *
     * @param string $table
     * @param int $id
     * @return mixed|null
     * @throws ServiceException
     */
    public static function refreshConfig(string $table, int $id)
    {
        return self::post(self::URI_REFRESH_CONFIG, ['table' => $table, 'id' => $id]);
    }
}
